#include "SceneParametersBuffer.h"
#include "Render/VulkanContext.h"
#include <vulkan/vulkan.hpp>

CSceneParameterBuffer::CSceneParameterBuffer()
{
    

}


void CSceneParameterBuffer::UpdateBuffer(const SSceneParameters &InParameters)
{
    Buffer.WriteData(InParameters);
}

void CSceneParameterBuffer::PrepareSelfRecord()
{
    SVulkanDescriptorPoolSize PoolSize;
    PoolSize.SetNum=1;
    PoolSize.DescriptorNums.UniformBufferDescriptorNum=1;
    CVulkanContext::Get()->GetAutoResetDescriptorPool()->GenerallPool.PoolSize+=PoolSize;
}

void CSceneParameterBuffer::UpdateDescriptorSet()
{
    //创建并写入描述集
    vk::DescriptorSetAllocateInfo DescriptorSetAllocateInfo;
    DescriptorSetAllocateInfo.descriptorPool = CVulkanContext::Get()->GetAutoResetDescriptorPool()->GenerallPool.Pool;
    DescriptorSetAllocateInfo.descriptorSetCount = 1;
    auto Layout = CVulkanContext::Get()->GetSceneGlobal()->SceneParameterLayout;
    DescriptorSetAllocateInfo.pSetLayouts = &Layout;
    DescriptorSet = CVulkanContext::Get()->GetDevice().allocateDescriptorSets(DescriptorSetAllocateInfo)[0];

    vk::DescriptorBufferInfo DescriptorBufferInfo;
    DescriptorBufferInfo.buffer = Buffer.GetBuffer();
    DescriptorBufferInfo.offset = 0;
    DescriptorBufferInfo.range = sizeof(SSceneParameters);

    vk::WriteDescriptorSet WriteDescriptorSet;
    WriteDescriptorSet.dstSet = DescriptorSet;
    WriteDescriptorSet.dstBinding = 0;
    WriteDescriptorSet.dstArrayElement = 0;
    WriteDescriptorSet.descriptorType = vk::DescriptorType::eUniformBuffer;
    WriteDescriptorSet.descriptorCount = 1;
    WriteDescriptorSet.pBufferInfo = &DescriptorBufferInfo;

    CVulkanContext::Get()->GetDevice().updateDescriptorSets(WriteDescriptorSet, nullptr);

}
